from multiprocessing.pool import Pool
from threading import Thread
from multiprocessing import Process
import time
from datetime import datetime
import requests


def request_url(url):
    return requests.get(url).status_code

def use_thread(url):
    t_array = []
    start = time.time()
    for i in range(10):
        t = Thread(target=request_url, args=(url, ))
        t.start()
        t_array.append(t)
    for t in t_array:
        t.join()
    end = time.time()
    return end - start

def use_process(url):
    p_array = []
    start = time.time()
    for i in range(10):
        t = Process(target=request_url, args=(url, ))
        t.start()
        p_array.append(t)
    for p in p_array:
        p.join()
    end = time.time()
    return end - start

def process_pool(url):
    pool = Pool(4)
    f_array = []
    start = time.time()
    for _ in range(10):
        future = pool.apply_async(request_url, args=(url,))
        f_array.append(future)
    pool.close()
    pool.join()
    end = time.time()
    return end - start

if __name__ == '__main__':
    print(use_thread('https://www.bilibili.com/'))
    print((use_process('https://www.bilibili.com/')))
    print(process_pool('https://www.bilibili.com/'))
